package com.weasel.security.escape.defaults;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Dylan on 2015/12/18.
 */
public class DefaultSensitiveWordEscapeService extends AbstractSensitiveWordEscapeService {

    private final Logger log = LoggerFactory.getLogger(getClass());

    private final Set<String> lexicons = new HashSet<>();

    {
        loadLexicons();
    }

    @Override
    protected Set<String> getLexicons() {
        return lexicons;
    }

    /**
     * @return
     * @throws IOException
     */
    private void loadLexicons(){
        try {
            File file = ResourceUtils.getFile("classpath:lexicon.txt");
            List<String> _lexicons = Files.readLines(file, Charsets.UTF_8);
            lexicons.addAll(_lexicons);
        } catch (Exception e) {
            log.error("读取敏感词库文件lexicon.txt失败");
        }
    }

}
